<template>
	<view class="AddressResidential">
		<mescroll-body height="100%" @init="mescrollInit" @down="$event=>$event.resetUpScroll(false)"
			@up="$event=> postPageList($event.num)" :toTop="{width: '100px',height: '100px'}" :down="{auto: false}"
			:up="{auto: true, textNoMore: '— 我也是有底线滴 —',empty:{use:false}}">
			<view class="residential color-9e size-28 font-weight-400">已开通的小区({{total || 0}}个)</view>
			<view class="address margin-bottom-16 por" v-for="(item,index) in list" :key="index"
				@click="onCommunity(item.id,item,index)">
				<view class="icons poa app-right" v-if="item.bool">
					<text class="color iconfont icon-xuanzhong1 size-50"></text>
				</view>
				<view class="margin-bottom-20 font-weight-500 color3 size-32">{{item.communityName}}</view>
				<view class="dis-cen color-9e size-28 font-weight-400">
					<text>{{item.province}}{{item.city}}{{item.county}}{{item.communityAddress}}</text>
					<text>{{item.distance}}km</text>
				</view>
			</view>
		</mescroll-body>
		<view class="add-goods app-bottom-left f5-background inset-bottom dis" v-if="options.type * 1 == 2">
			<u-button type="primary" @click="onConfirm" :disabled="disabled"
				:customStyle="{width: '690rpx',height: '96rpx',margin:'0 aotu', borderRadius: '100rpx'}"
				color="#EB0000">
				<text class="color-f font-weight-500 size-34 margin-left-12">确认</text>
			</u-button>
		</view>
		<view class="inset-bottom"></view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import { PageList, UserBindingCommunity } from "@/api/index"
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				list: [],
				loading: true,
				options: {},
				total: 0,
				params: {
					size: 10, // 	每页显示条数
					current: 1, // 当前页
					dataBean: {
						longitude: 0,
						latitude: 0
					}
				},
			}
		},
		async onLoad(options) {
			console.log('options', options)
			// type=2 是入驻审核 多选  type=1 是首页过来 点选
			// type =1首页点击过来的
			this.options = options
			// this.communityId = options.id
		},
		async onShow() {
			this.canReset && this.mescroll.resetUpScroll() // 重置列表数据为第一页
			this.canReset && this.mescroll.scrollTo(0, 0) // 滚动条也重置到顶部
			this.canReset = true // 过滤第一次的onShow事件
		},
		methods: {
			async postPageList(page) {
				console.log('page', page)
				const param = uni.getStorageSync('location')
				this.params.dataBean = param
				this.params.current = this.mescroll.num
				const { code, data } = await PageList(this.params)
				if (code == "200") {
					this.loading = false
					this.list = this.mescroll.num == 1 ? data.records : this.list.concat(data.records); //追加新数据
					this.total = data.total
					setTimeout(() => {
						this.loading = false
						this.mescroll.endByPage(data.records.length, data.total); //必传参数(当前页的数据个数, 总页数)
					}, 500)
				}
			},
			// 确认选择
			onConfirm() {
				let pages = getCurrentPages(); //获取当前页面
				let beforePage = pages[pages.length - 2] //获取上一个页面的实例
				const { list } = this
				let listOptions = []
				list.map(res => {
					if (res.bool) {
						let resData = {
							id: res.id,
							communityImage: res.img,
							communityName: res.communityName,
							communityAddress: res.communityAddress,
							communityHouseNumber: res.communityHouseNumber,
							communityLongitude: res.longitude,
							communityLatitude: res.latitude
						}
						listOptions.push(resData)
					}
				})
				if (listOptions.length * 1 > 5) {
					uni.$u.toast('最大只能选择5个小区', 2000)
					return
				}
				beforePage.$vm.params.infos = listOptions //调用上一页面方法
				uni.navigateBack({})
			},
			onCommunity(id, item, index) {
				console.log('this.options.type', this.options.type * 1)
				let pages = getCurrentPages(); //获取当前页面
				let beforePage = pages[pages.length - 2] //获取上一个页面的实例
				if (this.options.type * 1 == 2) { // 多选
					this.$set(this.list[index], 'bool', item.bool ? false : true)
					return
				}
				if (this.options.type * 1 == 1) {
					this.postUserBindingCommunity(id, item)
				} else {
					beforePage.$vm.params.communityId = id //调用上一页面方法
					beforePage.$vm.params.communityName = item.communityName //调用上一页面方法
					uni.navigateBack({})
				}
			},
			// 绑定小区
			async postUserBindingCommunity(id, item) {
				console.log('item', item)
				const { data, code } = await UserBindingCommunity({ communityId: id })
				if (code == '200') {
					const dataData = { id: item.id, communityName: item.communityName }
					uni.setStorageSync('village', dataData)
					uni.navigateBack({})
				}
			}
		}
	}
</script>

<style lang="scss">
	.AddressResidential {
		padding: 24rpx 24rpx 170rpx;

		.address {
			border-radius: 12rpx;
			overflow: hidden;
			background: #ffffff;
			padding: 24rpx;
		}

		.residential {
			margin-bottom: 24rpx;
		}

		.icons {
			top: 0;
		}

		.add-goods {
			height: 162rpx;
			width: 100%;
			position: fixed;
		}
	}
</style>